<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use cmf\controller\BaseController;
use think\Db;

	class WxloginController extends BaseController
	{
		
		function login(){
			// $obj=new RedisController;
			// $obj -> delete();
		// $data=array('appid'=>'wx0f912ae0a6bd22ea&secret=428d9485b1b696adc863e647ddcb29ce','js_code'=>'001mwwL92ZmAcO02PPL92NvuL92mwwLI','grant_type'=>'authorization_code');
		$url='http://v2.crejoy.cn/user/login/user_info';
		$token = $this->request->param('token');
		// db()
		$data['token']=$token;
		$info = sqcurl($url, 'post', $data, array());
		$info=json_decode($info,true);
		// echo count($info['data']['data']);
		// var_dump($info);
		// exit;
		$focus=db('focus')->where('app_userid',$info['data']['id'])->find();
		if($focus){
			db('focus')->update(array('is_focus'=>$info['data']['is_focus_gzh'],'app_userid'=>$info['data']['id'],'id'=>$focus['id']));
		}else{
			db('focus')->insert(array('is_focus'=>$info['data']['is_focus_gzh'],'app_userid'=>$info['data']['id']));
		}
		
		$staff_info=db('staff_member')->where('app_userid',$info['data']['id'])->field('id,business_card,staff_name,staff_phone,app_company_name,app_position')->find();
	
		//exit;
		if($staff_info){
		
			// return info_back(array('staff_id'=>$staff_id),'请求成功',true);
			return json_encode($staff_info);


		}else{




			// return info_back(array(),'请求失败',false);
			$add_info['app_userid']=$info['data']['id'];
			$add_info['app_user_type']=$info['data']['user_type'];
			$add_info['app_user_email']=$info['data']['user_email'];
			$add_info['app_sex']=$info['data']['sex'];
			$add_info['app_user_nickname']=$info['data']['user_nickname'];
			$add_info['app_staff_mobile']=$info['data']['mobile'];
			$add_info['app_create_time']=$info['data']['create_time'];
			$add_info['app_update_time']=$info['data']['update_time'];
			$add_info['app_area_id']=$info['data']['area_id'];
			$add_info['app_industry']=$info['data']['industry_id'];
			$add_info['app_position']=$info['data']['position'];
			$add_info['app_company_name']=$info['data']['company_name'];
			$add_info['app_company_tel']=$info['data']['company_tel'];
			$add_info['app_web_url']=$info['data']['web_url'];
			$add_info['app_logo']=$info['data']['logo'];
			$add_info['app_address']=$info['data']['address'];
			$add_info['app_business']=$info['data']['business'];
			$add_info['app_check_status']=$info['data']['check_status'];
			$add_info['app_user_login']=$info['data']['user_login'];
			$add_info['app_user_pass']=$info['data']['user_pass'];
			$add_info['app_user_status']=$info['data']['user_status'];
			$add_info['app_im_token']=$info['data']['im_token'];
			$add_info['app_accid']=$info['data']['accid'];
			$add_info['app_coin']=$info['data']['coin'];
			$add_info['app_avatar']=$info['data']['avatar'];
			$add_info['app_test_sign']=$info['data']['test_sign'];
			$add_info['app_receive_coin']=$info['data']['receive_coin'];
			$add_info['app_ali_id']=$info['data']['ali_id'];
			$add_info['app_ali_name']=$info['data']['ali_name'];
			$add_info['app_token']=$info['data']['token'];
			$add_info['app_limit_amount']=$info['data']['limit_amount'];
			$staff_id=db('staff_member')->insert($add_info);
			if($staff_id){
				$staff_info=db('staff_member')->where('app_userid',$info['data']['id'])->field('id,business_card,staff_name,staff_phone,app_company_name,app_position,app_avatar,customer_id')->find();
				// return db('staff_member')->getLastInsID();
				return json_encode($staff_info);


			}else{
				return '用户信息更新失败';
			}

		}
		
		// var_dump($add_info);

	}














































	function add_staff_info(){
		$data['id'] = $this->request->param('staff_id');
		$business_card = $this->request->param('business_card','');
		$form_id = $this->request->param('form_id','');
	
		if($business_card){
			$data['business_card']=$business_card;
			$data['apply_member']=3;
		}
		//$apply_data['is_audit'] = $this->request->param('is_audit',1);
		$staff_name=$this->request->param('staff_name');
		if($staff_name){
			$data['staff_name']=$staff_name;
		}
		$staff_phone=$this->request->param('staff_phone');
		if($staff_phone){
			$data['staff_phone']=$staff_phone;
		}
		//$app_company_name=$this->request->param('company_name');
		// if($app_company_name){
		// 	$data['app_company_name']=$app_company_name;
		// }
		$app_position=$this->request->param('position');
		if($app_position){
			$data['app_position']=$app_position;
		}
		$customer_id=$this->request->param('customer_id');
		if($customer_id){
			$data['customer_id']=$customer_id;
		}
		$apply_data['site_id']=$this->request->param('site_id');
		$apply_data['staff_id']=$this->request->param('staff_id');
		$apply_data['start_time']=$this->request->param('start_time');
		$apply_data['end_time']=$this->request->param('end_time');
		$apply_data['add_time']=time();
		$remark=$this->request->param('remark','');
		if($remark){
			$apply_data['remark']=$remark;
		}
		$apply_data['is_audit'] = $this->request->param('is_audit',1);
		if($apply_data['is_audit'] == 2){
			$apply_data['audit_status'] = 1;
		}else{
			$status=db('staff_member')->where('id',$data['id'])->value('apply_member');
			if($status == 2 || $status == 3){
				$apply_data['audit_status'] = 1;
			}else{
				$apply_data['audit_status'] = 2;
			}
			
		}
		// if($apply_data)
		// $where['start_time < ']= $apply_data['start_time'];
		// $where['end_time > ']= $apply_data['start_time'];
		// $where['start_time < ']= $apply_data['end_time'];
		// $where['end_time > ']= $apply_data['end_time'];
		// $where1['start_time > ']= $apply_data['start_time'];
		// $where1['end_time > ']= $apply_data['end_time'];
		
		if($apply_data['start_time'] > $apply_data['end_time']){
			$a = $apply_data['start_time'];
			$apply_data['start_time'] = $apply_data['end_time'];
			$apply_data['end_time'] = $a ;
		}
		
		$where='(start_time >  "'.$apply_data['start_time'].'" and start_time <  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and audit_status != 3) or (end_time >  "'.$apply_data['start_time'].'"  and end_time <  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and audit_status != 3 ) or  (start_time <=  "'.$apply_data['start_time'].'"  and end_time >=  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and audit_status != 3 ) or  (start_time >=  "'.$apply_data['start_time'].'"  and end_time <=  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and audit_status != 3 ) ';
		$distinc=db('site_apply_info')->where($where)->select();
		//var_dump($distinc);
		// var_dump($distinc);
		// /echo count($distinc);
		// if(count($distinc)){
		// 	return "该时间段已被预订预定";
		// }
		
		if(empty($data['id']) && empty($data['business_card']) &&  empty($data['staff_name']) && empty($data['staff_phone'])  && empty($data['app_position'])){
			return '确认预约信息不完整';
		}
		
		$sure = db('staff_member')->update($data);
		$apply=db('site_apply_info')->insert($apply_data);
		if($apply){
			 // file_put_contents('/hl/log.txt',print_r('11111',true),FILE_APPEND);
			if($apply_data['is_audit'] == 2){
				//echo 22222;

				$staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name')->find();
				
			//	var_dump($staff_info);
				$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
				// echo db('site')->getLastsql();
				$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
				// file_put_contents('/hl/log.txt',print_r('2222221',true),FILE_APPEND);
			
				 //var_dump($receive_info);
			//	echo $site_type_id .'###';
				// file_put_contents('/hl/log.txt',print_r('bbbbbbb',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($site_type_id['site_type_id'],true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r('ccccccc',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($receive_infos,true),FILE_APPEND);
				foreach ($receive_infos as $key => $receive_info) {
						# code...
					// file_put_contents('/hl/log.txt',print_r('aaaaqqq',true),FILE_APPEND);

					if(in_array($site_type_id['site_type_id'], explode(',', $receive_info['receive_site']))){
						// file_put_contents('/hl/log.txt',print_r('3333',true),FILE_APPEND);
						$first='有会议室预约请求待审核';
						$keyword1 = $staff_info['staff_name'];
						$keyword2 = $site_type_id['site_name'];
						$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
						$remark = '请尽快处理';
						$url = config('gz_send_templete');
						$url1 = config('meeting_audit_list').'?wx_secrect='.$data['id'];
							// file_put_contents('/hl/log.txt',print_r('**5555555555555555566666666677777778%%%%',true),FILE_APPEND);
							// file_put_contents('/hl/log.txt',print_r($url1,true),FILE_APPEND);
						$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
					}
				}
			}else{
				$staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name,s.apply_member')->find();
				if($staff_info['apply_member'] ==3){
					$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
				// echo db('site')->getLastsql();
					$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
					//file_put_contents('/hl/log.txt',print_r('*&*^^^121',true),FILE_APPEND);
					//file_put_contents('/hl/log.txt',print_r($receive_infos,true),FILE_APPEND);
					foreach ($receive_infos as $key => $receive_info) {
						# code...

					//var_dump($receive_info);
			//	echo $site_type_id .'###';
						if($receive_info['receive_user'] ==2 ){

							$first='有企业员工待审核';
							$keyword1 = $staff_info['staff_name'];
							$keyword2 = $site_type_id['site_name'];
							$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
							$remark = '请尽快处理';
							$url = config('gz_send_templete');
							$url1 =  config('staff_audit_list').'?wx_secrect='.$data['id'];
							// file_put_contents('/hl/log.txt',print_r('**5555555555555555566666666677777778%%%%',true),FILE_APPEND);
							// file_put_contents('/hl/log.txt',print_r($url1,true),FILE_APPEND);
							$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
							//file_put_contents('/hl/log.txt',print_r($return_info,true),FILE_APPEND);
						}
					}
				}
			}
			if($form_id){
				$site_name = db('site')->where('id',$apply_data['site_id'])->value('site_name');
				$app_userid = db('staff_member')->where('id',$data['id'])->value('app_userid');
				$is_focus = db('focus')->where('app_userid',$app_userid)->value('is_focus');
				if($is_focus != 1){
					$info =send_info($site_name,date("Y-m-d",$apply_data['start_time']).'--'.date("Y-m-d",$apply_data['end_time']),date("H:i",$apply_data['start_time']).'--'.date("H:i",$apply_data['end_time']),'预约','pages/mine/mine',$app_userid,$form_id);
		
				}
			}
			return 1;
		}else{
			return 0;
		}

	}
	function editStaff(){
		$data['app_avatar'] = $this->request->param('app_avatar');
		$data['id'] = $this->request->param('staff_id',0);
		$data['staff_name']=$this->request->param('staff_name');
		$data['staff_phone']=$this->request->param('staff_phone');
		// $data['app_company_name']=$this->request->param('company_name');
		$data['app_position']=$this->request->param('app_position');
		$data['customer_id']=$this->request->param('customer_id');
		$update=db('staff_member')->update($data);

		if($update){
			// return 1;
			return $this->get_userinfos($data['id']);
		}else{
			return 0;
		}
	
	}
	function tip(){
		$where=array();
		$arr=array();
		$str = $this->request->param('customer_name',''); 
		$incubator_id = $this->request->param('incubator_id',''); 
		if(!empty($incubator_id)){
			$where['incubator_id'] = $incubator_id;
		}else{
			$where['incubator_id'] =1;
		}
		$where['status']=1;
		$str_array=db('customer')->where('customer_name','like','%'.$str.'%')->where($where)->limit(10)->select();
		
		foreach ($str_array as $key => $value) {
			$arr[$key]['customer_id']=$value['id'];
			$arr[$key]['customer_name']=$value['customer_name'];
		}
		return json($arr);
		// $str1=implode(',', $arr);
		// echo $str1;  
	}




	function get_userinfos($staff_id){
		// $data['id'] = $this->request->param('staff_id');
		$data['id'] = $staff_id;
		$staff_info=db('staff_member')->where('id',$data['id'])->field('id,business_card,staff_name,staff_name,staff_phone,app_company_name,app_position,app_avatar,app_logo,customer_id')->find();
		if(!$staff_info){
			return '该员工不存在';
		}
		$staff_info['customer_name']=get_customer_name($staff_info['customer_id']);
		$where['staff_id'] = $data['id'];
		$where['audit_status'] = 2;
		$where1['staff_id'] = $data['id'];
		$where1['audit_status'] = 3;
		//$where2=' staff_id = "'.$data['staff_id'].'" and start_time > "'.time().'" ' ;
		$where2=' staff_id = "'.$data['id'].'" and start_time > "'.time().'" ' ;
		$condition1='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
		$condition=' start_time <= "'.time().'" and end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$arr['cancel_num']['num']= db('site_apply_info')->where($where1)->count();
		$arr['cancel_num']['name']='取消';
		$arr['cancel_num']['type']=3;
		$arr['using_num']['num']= db('site_apply_info')->where($condition)->where($condition1)->count();
		$arr['using_num']['name']='正在使用';
		$arr['using_num']['type']=2;
		$arr['allow_num']['num']=db('site_apply_info')->where($where2)->where($condition1)->count();
		$arr['allow_num']['name']='预订成功';
		$arr['allow_num']['type']=1;
		$staff_info=array_merge($staff_info,$arr);
		return json($staff_info);
	}

	function get_userinfo(){
		$data['id'] = $this->request->param('staff_id');
		$site_id = $this->request->param('site_id','');
		$staff_info=db('staff_member')->where('id',$data['id'])->field('id,business_card,staff_name,staff_name,staff_phone,app_company_name,app_position,app_avatar,app_logo,customer_id')->find();
		if(!$staff_info){
			return '该员工不存在';
		}
		if($site_id){
			// echo 2222;
			// exit;
			$site_address = db('site')->where('id',$site_id)->value('address');
			// var_dump($site_address);
			// exit;
			$staff_info['site_address'] = $site_address ;
			// var_dump($staff_info);
			// exit;
		}else{
			$staff_info['site_address'] = '暂无' ;
		}
		$staff_info['customer_name']=get_customer_name($staff_info['customer_id']);
		$where['staff_id'] = $data['id'];
		$where['audit_status'] = 2;
		$where1['staff_id'] = $data['id'];
		$where1['audit_status'] = 3;

		//$where2=' staff_id = "'.$data['staff_id'].'" and start_time > "'.time().'" ' ;
		$where2=' staff_id = "'.$data['id'].'" and start_time > "'.time().'" ' ;
		// $condition1='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
		$condition1=' audit_status =2 ';
		$condition=' start_time <= "'.time().'" and end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$condition2=' audit_status =1 ';
		$condition3='  end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$arr['cancel_num']['num']= db('site_apply_info')->where($where1)->count();
		$arr['cancel_num']['name']='取消';
		$arr['cancel_num']['type']=3;
		$arr['using_num']['num']= db('site_apply_info')->where($condition)->where($condition1)->count();
		$arr['using_num']['name']='正在使用';
		$arr['using_num']['type']=2;

		$arr['auditing_num']['num']= db('site_apply_info')->where($condition2)->where($condition3)->count();
		$arr['auditing_num']['name']='审核中';
		$arr['auditing_num']['type']=4;

		$arr['allow_num']['num']=db('site_apply_info')->where($where2)->where($condition1)->count();
		$arr['allow_num']['name']='预订成功';
		$arr['allow_num']['type']=1;
		$staff_info=array_merge($staff_info,$arr);
		return json($staff_info);
	}















function test(){
	if($_POST){
		$b='';
		$a=[1,23,4,56,7,8];
		$b.='<form action="" method="post">
		服务器地址';
		foreach ($a as $key => $value) {
			$b.='<input type="text" name="host" value="'.$value.'" />';
		}


		
		$b.='<input type="text" name="host" value="localhost" /><br />
		数据库账号：<input type="text" name="user" value="" /><br />
		数据库密码：<input type="password" name="pwd" value="" /><br />
		指定数据库：<input type="text" name="db" value="test" /><br />
		<input type="submit" value="确定"/>
	    </form>';
	    echo $b;
    }
}



function bind(){

	$back_info = $_REQUEST;
			// var_dump($back_info);
			// exit;
	$code=$back_info['code'];
			//$code = '011nQ18Q1qjUO71UXbaQ1Zxn8Q1nQ18v';
	$url = "http://v2.crejoy.cn/api/login/check_login_wx?code=$code&type=2";
			//echo $url;
	$info = sqcurl($url, 'get', array(), array());
	$arr = json_decode(json_encode(json_decode($info)),true);
	if(!empty($back_info['ext'])){
		header("Content-Type: text/html; charset=utf8");
		echo '<div width="50%" height="50%" vertical-align="middle" align="center"><span>'.$arr['data']['user_nickname'].',请长按二维码在弹出菜单中选择识别图中二维码关注创享公众号</span><br/><img width="500px" height="500px" src="https://img.crejoy.cn/qrcode_for_crejoy_service_platform.jpg"/></div>';
		exit;
	}
	
			//var_dump($arr['data']);
	$data['id'] = $back_info['user_id'];
	$data['app_userid'] = $arr['data']['id'] ;
	$is_has = db('user')->where('id',$back_info['user_id'])->find();
	if($is_has['app_userid']){
		if($is_has['app_userid'] == $arr['data']['id']){

			return '已经绑定，无需重复绑定';
		}
		$data['wx_nickname'] = $arr['data']['user_nickname'];
		$update=db('user')->update($data);
		return '已经更绑';
	}
	$data['wx_nickname'] = $arr['data']['user_nickname'];
	$update=db('user')->update($data);
	if($update){
		header("Content-Type: text/html; charset=utf8");
		return '绑定成功。 <div width="50%" height="50%" vertical-align="middle" align="center"><span>'.$arr['data']['user_nickname'].',请长按二维码在弹出菜单中选择识别图中二维码关注创享公众号</span><br/><img width="500px" height="500px" src="https://img.crejoy.cn/qrcode_for_crejoy_service_platform.jpg"/></div>';
	}else{
		return '绑定失败';
	}
			//db('incubator')->
			// var_dump($arr);



}















   }